package com.example.service;

import com.example.entity.Order;
import io.camunda.zeebe.client.api.response.ActivatedJob;
import io.camunda.zeebe.spring.client.annotation.JobWorker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 发货审批工作器（Camunda 人工任务，此处简化为自动审批）
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeliveryService {

    private final OrderService orderService;

    @JobWorker(type = "approve-delivery", autoComplete = true)
    public void approveDelivery(ActivatedJob job) {
        String orderNo = job.getVariablesAsMap().get("orderNo").toString();
        log.info("开始处理订单发货审批：{}", orderNo);

        // 1. 模拟人工审批（实际场景通过 Camunda Tasklist 分配给审批人）
        boolean approvePass = true;  // 模拟审批通过
        if (!approvePass) {
            throw new RuntimeException("订单发货审批驳回：" + orderNo);
        }

        // 2. 更新订单状态为「已发货」
        orderService.updateOrderStatus(orderNo, Order.OrderStatus.DELIVERED);
        log.info("订单发货审批通过，状态更新为 DELIVERED：{}", orderNo);
    }
}